package com.sk.commonadapter_android;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.sk.commonadapter_android.adapter.CommonRecyclerViewAdapter;
import com.sk.commonadapter_android.adapter.CommonViewHolder;
import com.sk.commonadapter_android.adapter.RefreshRecyclerView;
import com.sk.commonadapter_android.adapter.refresh.LoadMoreLayout;
import com.sk.commonadapter_android.adapter.refresh.RefreshHeaderLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author yemao
 * @Email yrmao9893@163.com
 * @Date 2017/7/5
 * @Des null!
 */

public class RefreshRecyclerViewActivity extends AppCompatActivity  implements RefreshRecyclerView.PullLinstener{
    private RefreshRecyclerView mRefreshRecyclerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refresh_recyclerview);
        mRefreshRecyclerView= (RefreshRecyclerView) findViewById(R.id.recyclerview_refresh);
        mAdapter.addAll(getData());
        mAdapter.notifyDataSetChanged();
        mRefreshRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,RecyclerView.VERTICAL));
        mRefreshRecyclerView.setAdapter(mAdapter);
        mRefreshRecyclerView.addItemDecoration(new DividerItemDecoration(this,RecyclerView.VERTICAL));
        mRefreshRecyclerView.setLoadMoerEnabled(false);
        mRefreshRecyclerView.setPullRefreshEnabled(true);
        mRefreshRecyclerView.setPullLinstener(this);
        mRefreshRecyclerView.addHeaderView(new LoadMoreLayout(this));
        mRefreshRecyclerView.addHeaderView(new LoadMoreLayout(this));
    }
    private List<String> getData(){
        List<String> mData=new ArrayList<>();
        for (int i=0;i<10;i++){
            mData.add(""+i);
        }
        return mData;
    }
    CommonRecyclerViewAdapter<String> mAdapter=new CommonRecyclerViewAdapter<String>(this,R.layout.item_common_recyclerview) {
        @Override
        public void convert(CommonViewHolder holder, String item, int position) {

        }
    };

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            public void run() {
                mAdapter.clear();
                mAdapter.addAll(getData());
                mAdapter.notifyDataSetChanged();
                mRefreshRecyclerView.onRefreshComlete();
            }
        }, 2000);

    }

    @Override
    public void onLoadMore() {
        if (mAdapter.getItemCount()<10)
           new AsyncTask<String, String, List<String>>() {

            @Override
            protected List<String> doInBackground(String... params) {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return getData();
            }

            @Override
            protected void onPostExecute(List<String> strings) {
                super.onPostExecute(strings);
                mAdapter.addAll(strings);
                mAdapter.notifyDataSetChanged();
                mRefreshRecyclerView.onLoadMoreComlete();
            }
        }.execute();
        else
           mRefreshRecyclerView.onLoadMoreComlete();
    }
}
